<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/


/*
 * 威望类
 */
class reputation_class extends AWS_MODEL
{
	
	//获取用户话题威望
	public function get_reputation_topic($uid)
	{
		return ($this->fetch_all('reputation_topic', ' uid = ' . intval($uid), ' topic_count DESC '));
	}

	public function cron($start = 0, $limit = 100)
	{
		//取出用户
		if ($start == 0)
		{
			$start = $this->model('db_cache')->load('reputation_cron_user_start');

			$start = $start * 1;
		}
		
		$users_list = $this->model('account')->get_user_list('', $start . ',' . $limit);
		
		//	p($users_list);
		
		$reputation_function = get_setting('reputation_function');
		
		if (! $users_list)
		{
			$users_list = array();
		}
		foreach ($users_list as $key => $val)
		{
			
			$user_topics = null;
			$user_reputation = 0;
			//取出用户回复
			$users_anwser = $this->model('answer')->get_answers(false, 'uid=' . $val['uid']);
			
			if ($users_anwser)
			{
				
				foreach ($users_anwser as $an_key => $an_val)
				{
					
					//$reputation_function=get_setting('reputation_function');
					

					$best_answer_reput = get_setting('best_answer_reput');
					$publisher_reputation_factor = get_setting('publisher_reputation_factor');
					//$publisher_agaist_reput = get_setting('publisher_agaist_reput');
					
					//[最佳答案]+[赞同]-[反对]+[发起者赞同]-[发起者反对];
					

					$answer_id = $an_val['answer_id'];
					$question_id = $an_val['question_id'];
					
					$s_best_answer = 0;
					$s_agree_count = 0;
					$s_against_count = 0;
					$s_initiate_agree_count = 0;
					$s_initiate_against_count = 0;
					$tmp_reputation = 0;
					
					$question = $this->model('question')->get_question_info_by_id($question_id);
					
					if (! $question)
					{
						continue;
					}
					
					//获取问题的话题
					

					$topics = $this->model('question_topic')->get_question_topic_by_question_id($question_id);
					
					if ($topics)
					{
						foreach ($topics as $topic)
						{
							
							if (! $topic['topic_id'])
							{
								continue;
							}
							$count = $user_topics[$topic['topic_id']]['count'] * 1 + 1;
							$user_topics[$topic['topic_id']] = array(
								'topic_id' => $topic['topic_id'], 
								'count' => $count
							);
						}
					}
					
					if ($question['best_answer'] == $answer_id)
					{
						$s_best_answer = 1;
					}
					
					//$s_agree_count	=$an_val['agree_count'];
					//$s_against_count=$an_val['against_count'];
					

					$s_agree_count = 0;
					$s_against_count = 0;
					
					if($vote_users = $this->model('answer')->get_vote_user_by_answer_id($answer_id))					
					{
						foreach ($vote_users as $vote_key => $vote_val)
						{
							if ($question['published_uid'] == $vote_val['uid'])
							{
								continue;
							}
							
							$s_agree_count += $s_agree_count + $vote_val['reputation_factor'];
						
						}
					}
					
					if ($vote_users)
					{
						//p($vote_users);
						if (array_key_exists($question['published_uid'], $vote_users))
						{
							$s_initiate_agree_count = 1;
						}
					}
					
					
					if($vote_against_users = $this->model('answer')->get_vote_against_user_by_answer_id($answer_id))
					{
						foreach ($vote_against_users as $vote_key => $vote_val)
						{
							if ($question['published_uid'] == $vote_val['uid'])
							{
								continue;
							}
							
							$s_against_count += $s_against_count + $vote_val['reputation_factor'];
						
						}
					}
					
					if ($vote_against_users)
					{
						if (array_key_exists($question['published_uid'], $vote_against_users))
						{
							$s_initiate_against_count = 1;
						}
					}
					
					if ($s_initiate_agree_count > 0)
					{
						$s_agree_count = $s_agree_count - 1;
					}
					
					if ($s_initiate_against_count > 0)
					{
						$s_against_count = $s_against_count - 1;
					}
					
					//[最佳答案]*3+[赞同]*1-[反对]*1+[发起者赞同]*2-[发起者反对]*1
					//$reputation_function=str_replace('[最佳答案]', $s_best_answer, $reputation_function);
					//$reputation_function=str_replace('[赞同]', $s_agree_count, $reputation_function);
					//$reputation_function=str_replace('[反对]', $s_against_count, $reputation_function);
					//$reputation_function=str_replace('[发起者赞同]', $s_initiate_agree_count, $reputation_function);
					//$reputation_function=str_replace('[发起者反对]', $s_initiate_against_count, $reputation_function);
					

					$tmp_reputation = $s_best_answer * $best_answer_reput + $s_agree_count - $s_against_count + $s_initiate_agree_count * $publisher_reputation_factor - $s_initiate_against_count * $publisher_reputation_factor;
					
					$tmp_reputation = log($tmp_reputation, 3);
					
					//eval('$tmp_reputation='.$reputation_function.';');
					
					if (intval($tmp_reputation) <= 0)
					{
						$tmp_reputation = 0;
					}
					
					$user_reputation = $user_reputation + $tmp_reputation; //$s_best_answer+$s_agree_count-$s_against_count+$s_initiate_agree_count-$s_initiate_against_count;
				}
			}
			
			if (is_array($user_topics))
			{
				$user_topics = aasort($user_topics, array(
					'-count'
				));
				
				if ($user_topics)
				{
					$user_topics = array_slice($user_topics, 0, 20);
				}
				else
				{
					$user_topics = array();
				}
				
				$this->delete('reputation_topic', 'uid = ' . $val['uid']);
				
				foreach ($user_topics as $t_key => $t_val)
				{
					
					$insert_array = null;
					$insert_array['uid'] = $val['uid'];
					$insert_array['topic_id'] = $t_val['topic_id'];
					$insert_array['topic_count'] = $t_val['count'];
					$insert_array['update_time'] = time();
					
					$this->insert('reputation_topic', $insert_array);
				}
			}
			
			//写入数据库
			$this->model('account')->update_users_fields(array(
				'reputation' => $user_reputation, 
				'reputation_update_time' => time()
			), $val['uid']);
			
			$this->model('account')->update_user_reputation_group($val['uid']);
		
		}
		
		//重置数值
		if (count($users_list) < $limit)
		{
			$start = 0;
		}
		else
		{
			$start = $start + $limit;
		}
		
		//p('___' . $start);
		$this->model('db_cache')->save('reputation_cron_user_start', $start, 60 * 60 * 24);
	}

}
	